<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.1.1">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/avatar.jpg">
  <link rel="mask-icon" href="/images/avatar.jpg" color="#222">
  <meta name="msvalidate.01" content="4AF2328B2035748153A041B7EEF3D15C">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Noto+Serif+SC:300,300italic,400,400italic,700,700italic%7CLobster:300,300italic,400,400italic,700,700italic%7CPingFang+SC:300,300italic,400,400italic,700,700italic%7CRoboto+Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.33/fancybox/fancybox.css" integrity="sha256-gkQVf8UKZgQ0HyuxL/VnacadJ+D2Kox2TCEBuNQg5+w=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"ethanliu6.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":true,"sidebar":{"position":"left","display":"always","padding":12,"offset":18},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":true,"color":"#222","save":"manual"},"mediumzoom":true,"lazyload":true,"pangu":false,"comments":{"style":"buttons","active":"changyan","storage":true,"lazyload":false,"nav":null,"activeClass":"changyan"},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":6,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="JavaSE基础——Day07 该篇主要讲解Java中的常用API:String、Integer、Date、SimpleDateFormat">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaSE基础——Day07 JavaSE基础——常用API:String、Integer、Date、SDF">
<meta property="og:url" content="http://ethanliu6.github.io/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/index.html">
<meta property="og:site_name" content="&lt;font color&#x3D;&#39;white&#39; &gt;Ethan の Blog&lt;&#x2F;font&gt;">
<meta property="og:description" content="JavaSE基础——Day07 该篇主要讲解Java中的常用API:String、Integer、Date、SimpleDateFormat">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/imgs/1640164265984.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/imgs/1640164272200.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/imgs/1640164638518.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/imgs/1640164718346.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/imgs/1640164796396.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/imgs/1640164856732.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/imgs/1640165180138.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/imgs/1640165405894.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/imgs/1640165411817.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/imgs/1640165745241.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/imgs/1640165873028.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/imgs/1640165952879.png">
<meta property="article:published_time" content="2024-03-20T16:00:00.000Z">
<meta property="article:modified_time" content="2024-04-26T05:13:37.434Z">
<meta property="article:author" content="Ethan Liu">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="Java基础">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://ethanliu6.github.io/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/imgs/1640164265984.png">


<link rel="canonical" href="http://ethanliu6.github.io/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://ethanliu6.github.io/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/","path":"2024/03/21/JavaNotes/JavaSE基础/day07常用API(2)/","title":"JavaSE基础——Day07 JavaSE基础——常用API:String、Integer、Date、SDF"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>JavaSE基础——Day07 JavaSE基础——常用API:String、Integer、Date、SDF | <font color='white' >Ethan の Blog</font></title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<style>.darkmode--activated{--body-bg-color:#282828;--content-bg-color:#333;--card-bg-color:#555;--text-color:#ccc;--blockquote-color:#bbb;--link-color:#ccc;--link-hover-color:#eee;--brand-color:#ddd;--brand-hover-color:#ddd;--table-row-odd-bg-color:#282828;--table-row-hover-bg-color:#363636;--menu-item-bg-color:#555;--btn-default-bg:#222;--btn-default-color:#ccc;--btn-default-border-color:#555;--btn-default-hover-bg:#666;--btn-default-hover-color:#ccc;--btn-default-hover-border-color:#666;--highlight-background:#282b2e;--highlight-foreground:#a9b7c6;--highlight-gutter-background:#34393d;--highlight-gutter-foreground:#9ca9b6}.darkmode--activated img{opacity:.75}.darkmode--activated img:hover{opacity:.9}.darkmode--activated code{color:#69dbdc;background:0 0}button.darkmode-toggle{z-index:9999}.darkmode-ignore,img{display:flex!important}.beian img{display:inline-block!important}</style></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title"><font color='white' >Ethan の Blog</font></p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description"><font color='#FDABC1' size=3>清醒的停留，胜过——盲目的前行</font></p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-关于我"><a href="/aboutme/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">18</span></a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">14</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">53</span></a></li><li class="menu-item menu-item-love"><a href="/categories/Love/" rel="section"><i class="fas fa-heart fa-fw"></i>Love</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%B8%B8%E7%94%A8api2"><span class="nav-text">常用API(2)</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#string"><span class="nav-text">1：String</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#string%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95"><span class="nav-text">1.1 String(构造方法)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#string%E6%88%90%E5%91%98%E6%96%B9%E6%B3%95"><span class="nav-text">1.2 String(成员方法)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#gui%E6%A1%88%E4%BE%8B1%E7%94%A8%E6%88%B7%E7%99%BB%E5%BD%95%E5%AE%9E%E7%8E%B0"><span class="nav-text">1.3 GUI案例1(用户登录实现)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#gui%E6%A1%88%E4%BE%8B2%E8%81%8A%E5%A4%A9%E5%AE%A4%E5%AE%9E%E7%8E%B0"><span class="nav-text">1.4 GUI案例2(聊天室实现)</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#integer"><span class="nav-text">2：Integer</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#integer%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95"><span class="nav-text">2.1 Integer(构造方法)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#integerint%E5%92%8Cstring%E7%9A%84%E7%9B%B8%E4%BA%92%E8%BD%AC%E6%8D%A2"><span class="nav-text">2.2
Integer(int和String的相互转换)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#gui%E6%A1%88%E4%BE%8B3%E7%8C%9C%E6%95%B0%E5%AD%97%E5%AE%9E%E7%8E%B0"><span class="nav-text">2.3 GUI案例3(猜数字实现)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#integer%E8%87%AA%E5%8A%A8%E8%A3%85%E7%AE%B1%E5%92%8C%E6%8B%86%E7%AE%B1"><span class="nav-text">2.4 Integer(自动装箱和拆箱)</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#date%E5%92%8Csimpledateformat"><span class="nav-text">3：Date和SimpleDateFormat</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#date%E6%9E%84%E9%80%A0%E6%96%B9%E6%B3%95"><span class="nav-text">3.1 Date(构造方法)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#simpledateformatdate%E5%92%8Cstring%E7%9A%84%E7%9B%B8%E4%BA%92%E8%BD%AC%E6%8D%A2"><span class="nav-text">3.2
SimpleDateFormat(Date和String的相互转换)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#gui%E6%A1%88%E4%BE%8B4%E6%89%8B%E6%9C%BA%E6%97%A5%E6%9C%9F%E5%92%8C%E6%97%B6%E9%97%B4%E6%98%BE%E7%A4%BA%E5%AE%9E%E7%8E%B0"><span class="nav-text">3.3
GUI案例4(手机日期和时间显示实现)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#gui%E6%A1%88%E4%BE%8B5%E8%80%83%E5%8B%A4%E6%9F%A5%E8%AF%A2%E5%AE%9E%E7%8E%B0"><span class="nav-text">3.4 GUI案例5(考勤查询实现)</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Ethan Liu"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Ethan Liu</p>
  <div class="site-description" itemprop="description"><font color='#0000FF' size=1.5px>语言学 | NLP | Py | Java</font></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">53</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2V0aGFubGl1Ng==" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;ethanliu6"><i class="fab fa-github fa-fw"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="bWFpbHRvOmxpdWUyMzM2QGdtYWlsLmNvbQ==" title="E-Mail → mailto:liue2336@gmail.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9zcGFjZS5iaWxpYmlsaS5jb20vMTMyNzA5OTk3Nw==" title="BiliBili → https:&#x2F;&#x2F;space.bilibili.com&#x2F;1327099977"><i class="fa-brands fa-bilibili fa-fw"></i>BiliBili</span>
      </span>
      <span class="links-of-author-item">
        <a href="/categories/Love/" title="Love → &#x2F;categories&#x2F;Love&#x2F;" rel="noopener me"><i class="fa fa-heart fa-fw"></i>Love</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://ethanliu6.github.io/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Ethan Liu">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="<font color='white' >Ethan の Blog</font>">
      <meta itemprop="description" content="<font color='#0000FF' size=1.5px>语言学 | NLP | Py | Java</font>">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="JavaSE基础——Day07 JavaSE基础——常用API:String、Integer、Date、SDF | <font color='white' >Ethan の Blog</font>">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JavaSE基础——Day07 JavaSE基础——常用API:String、Integer、Date、SDF<span class="exturl post-edit-link" data-url="aHR0cHM6Ly9naXRodWIuY29tL0V0aGFuTGl1Ni9ldGhhbmxpdTYuZ2l0aHViLmlvL3RyZWUvbWFzdGVyL19wb3N0cy9KYXZhTm90ZXMvSmF2YVNF5Z+656GAL2RheTA35bi455SoQVBJKDIpLm1k" title="编辑"><i class="fa fa-pen-nib"></i></span>
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2024-03-21 00:00:00" itemprop="dateCreated datePublished" datetime="2024-03-21T00:00:00+08:00">2024-03-21</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2024-04-26 13:13:37" itemprop="dateModified" datetime="2024-04-26T13:13:37+08:00">2024-04-26</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java%E5%85%A8%E6%A0%88/" itemprop="url" rel="index"><span itemprop="name">Java全栈</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    <a title="JavaSE基础——Day07 JavaSE基础——常用API:String、Integer、Date、SDF" href="/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/#SOHUCS" itemprop="discussionUrl">
      <span id="sourceId::44fc5a735838d3c51243aaf6d238fea3" class="cy_cmt_count" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>3.4k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>12 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><blockquote class="blockquote-center">
<p>JavaSE基础——Day07
该篇主要讲解Java中的<strong>常用API:String、Integer、Date、SimpleDateFormat</strong></p>

</blockquote>
<span id="more"></span>
<h1 id="常用api2">常用API(2)</h1>
<p><strong>今日目标：</strong></p>
<ul>
<li>能够使用String类的功能完成用户登录案例</li>
<li>能够使用String类的功能完成聊天室案例</li>
<li>能够使用Integer类的功能完成猜数字案例</li>
<li>能够使用Date类和SimpleDateFormat类的功能完成手机日期和时间显示案例</li>
<li>能够使用Date类和SimpleDateFormat类的功能完成考勤查询案例</li>
</ul>
<h2 id="string">1：String</h2>
<h3 id="string构造方法">1.1 String(构造方法)</h3>
<p>来，继续啊，前面我们做过了GUI中的案例，也就是把一些小应用的界面做好了，但是具体功能并没有实现。接下来呢，我们就来一起实现一下。</p>
<p>首先，我们先来学习String类，它涉及到两个案例：用户登录和聊天室。</p>
<p>先来看用户登录案例：需要输入用户名和密码，和已知的用户名和密码进行比较，涉及到比较的方法，</p>
<p>一般来说，用户名和密码的长度可能会有一定限制，涉及到获取字符串长度的方法，</p>
<figure>
<img data-src="imgs/1640164265984.png" alt="1640164265984">
<figcaption aria-hidden="true">1640164265984</figcaption>
</figure>
<p>再来看聊天室案例，这里输入聊天的内容，然后把聊天内容显示到上面的文本域。</p>
<p>在聊天的时候，可能不小心打了空格，我们不需要的，需要去掉前后空格，</p>
<figure>
<img data-src="imgs/1640164272200.png" alt="1640164272200">
<figcaption aria-hidden="true">1640164272200</figcaption>
</figure>
<p>当然了，还有很多其他的成员方法，只不过，目前我们暂时用不上，后面用的时候再讲。</p>
<p>下面，我们就来学习一下字符串类String，最后完成用户登录案例和聊天室案例。</p>
<p>而关于String类呢，我们先到帮助文档中去查看，然后回来总结，最后到IDEA中去演示。</p>
<p>来，打开我们准备好的帮助文档，关于通过帮助文档查看String，大家按照老师讲解的API的使用步骤看看即可。</p>
<p>通过文档了解了String类之后，下面我们回到资料中总结一下：</p>
<p><strong>String：</strong></p>
<ul>
<li>String类表示字符串。 Java程序中的所有字符串文字（例如"abc"
）都实现为此类的实例</li>
</ul>
<p><strong>构造方法：</strong></p>
<ul>
<li>String()：初始化新创建的 String对象，使其表示空字符序列</li>
<li>String(String
original)：初始化新创建的String对象，使其表示与参数相同的字符序列</li>
</ul>
<p>知道了String的构造方法后，我们到IDEA中去使用一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    String类表示字符串。 Java程序中的所有字符串文字（例如&quot;abc&quot; ）都实现为此类的实例</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    构造方法：</span></span><br><span class="line"><span class="comment">        String()：初始化新创建的 String对象，使其表示空字符序列</span></span><br><span class="line"><span class="comment">        String(String original)：初始化新创建的String对象，使其表示与参数相同的字符序列</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StringDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//String()：初始化新创建的 String对象，使其表示空字符序列</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">String</span>();</span><br><span class="line">        System.out.println(s1);</span><br><span class="line">        <span class="comment">//int length() 返回此字符串的长度</span></span><br><span class="line">        System.out.println(s1.length());</span><br><span class="line">        System.out.println(<span class="string">&quot;------------&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//String(String original)：初始化新创建的String对象，使其表示与参数相同的字符序列</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">String</span>(<span class="string">&quot;itheima&quot;</span>);</span><br><span class="line">        System.out.println(s2);</span><br><span class="line">        System.out.println(s2.length());</span><br><span class="line">        System.out.println(<span class="string">&quot;------------&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//Java程序中的所有字符串文字（例如&quot;abc&quot; ）都实现为此类的实例</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s3</span> <span class="operator">=</span> <span class="string">&quot;itheima&quot;</span>;</span><br><span class="line">        System.out.println(s3);</span><br><span class="line">        System.out.println(s3.length());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h3 id="string成员方法">1.2 String(成员方法)</h3>
<p>来，继续啊，下面我们来学习String类中常用的成员方法：</p>
<ul>
<li>int length()：返回此字符串的长度</li>
<li>boolean equals(Object anObject)：将此字符串与指定的对象进行比较</li>
<li>boolean equalsIgnoreCase(String anotherString)：将此 String与另一个
String比较，忽略了大小写</li>
<li>String
trim()：返回一个字符串，其值为此字符串，删除了所有前导和尾随空格</li>
</ul>
<p>而关于这几个成员方法呢，我们到IDEA中一边使用，一边讲解。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    int length()：返回此字符串的长度</span></span><br><span class="line"><span class="comment">    boolean equals(Object anObject)：将此字符串与指定的对象进行比较</span></span><br><span class="line"><span class="comment">    boolean equalsIgnoreCase(String anotherString)：将此 String与另一个 String比较，忽略了大小写</span></span><br><span class="line"><span class="comment">    String trim()：返回一个字符串，其值为此字符串，删除了所有前导和尾随空格</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StringDemo02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">String</span> <span class="variable">s1</span> <span class="operator">=</span> <span class="string">&quot;itheima&quot;</span>;</span><br><span class="line">        <span class="type">String</span> <span class="variable">s2</span> <span class="operator">=</span> <span class="string">&quot;itheima&quot;</span>;</span><br><span class="line">        <span class="type">String</span> <span class="variable">s3</span> <span class="operator">=</span> <span class="string">&quot;Itheima&quot;</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//boolean equals(Object anObject)：将此字符串与指定的对象进行比较</span></span><br><span class="line">        System.out.println(s1.equals(s2));</span><br><span class="line">        System.out.println(s1.equals(s3));</span><br><span class="line"></span><br><span class="line">        <span class="comment">//boolean equalsIgnoreCase(String anotherString)：将此 String与另一个 String比较，忽略了大小写</span></span><br><span class="line">        System.out.println(s1.equalsIgnoreCase(s2));</span><br><span class="line">        System.out.println(s1.equalsIgnoreCase(s3));</span><br><span class="line">        System.out.println(<span class="string">&quot;-------------------&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">String</span> <span class="variable">s4</span> <span class="operator">=</span> <span class="string">&quot; itheima &quot;</span>;</span><br><span class="line">        <span class="comment">//String trim()：返回一个字符串，其值为此字符串，删除了所有前导和尾随空格</span></span><br><span class="line">        System.out.println(s4);</span><br><span class="line">        System.out.println(s4.trim());</span><br><span class="line"></span><br><span class="line">        <span class="type">String</span> <span class="variable">s5</span> <span class="operator">=</span> <span class="string">&quot;it heima&quot;</span>;</span><br><span class="line">        System.out.println(s5.trim());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h3 id="gui案例1用户登录实现">1.3 GUI案例1(用户登录实现)</h3>
<p>需求：根据下列要求，完成用户登录的逻辑，并给出相应的提示</p>
<figure>
<img data-src="imgs/1640164638518.png" alt="1640164638518">
<figcaption aria-hidden="true">1640164638518</figcaption>
</figure>
<p>要求：</p>
<p>① 已知的用户名和密码</p>
<pre><code>用户名：itheima

密码：123456</code></pre>
<p>② 用户名和密码的长度都是6-12位</p>
<p>③ 点击登录按钮</p>
<pre><code>先判断输入的用户名和密码是否符合要求

再判断用户登录是否成功</code></pre>
<p>知道了用户登录案例的需求和要求后，下面我们到IDEA中去实现一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionEvent;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionListener;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    1:已知的用户名和密码</span></span><br><span class="line"><span class="comment">        用户名：itheima</span></span><br><span class="line"><span class="comment">       密码：123456</span></span><br><span class="line"><span class="comment">    2:用户名和密码的长度都是6-12位</span></span><br><span class="line"><span class="comment">    3:点击登录按钮</span></span><br><span class="line"><span class="comment">       先判断输入的用户名和密码是否符合要求</span></span><br><span class="line"><span class="comment">       再判断用户登录是否成功</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">UserLogin</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建窗体对象</span></span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">jf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>();</span><br><span class="line"></span><br><span class="line">        jf.setTitle(<span class="string">&quot;用户登录&quot;</span>);</span><br><span class="line">        jf.setSize(<span class="number">400</span>, <span class="number">300</span>);</span><br><span class="line">        jf.setDefaultCloseOperation(<span class="number">3</span>);</span><br><span class="line">        jf.setLocationRelativeTo(<span class="literal">null</span>);</span><br><span class="line">        jf.setAlwaysOnTop(<span class="literal">true</span>);</span><br><span class="line">        jf.setLayout(<span class="literal">null</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//显示用户名文本</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">usernameLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;用户名&quot;</span>);</span><br><span class="line">        usernameLable.setBounds(<span class="number">50</span>, <span class="number">50</span>, <span class="number">50</span>, <span class="number">20</span>);</span><br><span class="line">        jf.add(usernameLable);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//用户名输入框</span></span><br><span class="line">        <span class="type">JTextField</span> <span class="variable">usernameField</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JTextField</span>();</span><br><span class="line">        usernameField.setBounds(<span class="number">150</span>, <span class="number">50</span>, <span class="number">180</span>, <span class="number">20</span>);</span><br><span class="line">        jf.add(usernameField);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//显示密码文本</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">passwordLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;密码&quot;</span>);</span><br><span class="line">        passwordLable.setBounds(<span class="number">50</span>, <span class="number">100</span>, <span class="number">50</span>, <span class="number">20</span>);</span><br><span class="line">        jf.add(passwordLable);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//密码输入框</span></span><br><span class="line">        <span class="type">JPasswordField</span> <span class="variable">passwordField</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JPasswordField</span>();</span><br><span class="line">        passwordField.setBounds(<span class="number">150</span>, <span class="number">100</span>, <span class="number">180</span>, <span class="number">20</span>);</span><br><span class="line">        jf.add(passwordField);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//登录按钮</span></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">loginButton</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">&quot;登录&quot;</span>);</span><br><span class="line">        loginButton.setBounds(<span class="number">50</span>, <span class="number">200</span>, <span class="number">280</span>, <span class="number">20</span>);</span><br><span class="line">        jf.add(loginButton);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//已知的用户名和密码</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> <span class="string">&quot;itheima&quot;</span>;</span><br><span class="line">        <span class="type">String</span> <span class="variable">pwd</span> <span class="operator">=</span> <span class="string">&quot;123456&quot;</span>;</span><br><span class="line"></span><br><span class="line">        loginButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">                <span class="comment">//获取用户输入的用户名和密码</span></span><br><span class="line">                <span class="type">String</span> <span class="variable">username</span> <span class="operator">=</span> usernameField.getText();</span><br><span class="line">                <span class="type">String</span> <span class="variable">password</span> <span class="operator">=</span> passwordField.getText();</span><br><span class="line"></span><br><span class="line">                <span class="comment">//先判断输入的用户名和密码是否符合要求</span></span><br><span class="line">                <span class="comment">//用户名和密码的长度都是6-12位</span></span><br><span class="line">                <span class="keyword">if</span>(username.length()&lt;<span class="number">6</span> || username.length()&gt;<span class="number">12</span>) &#123;</span><br><span class="line"><span class="comment">//                    System.out.println(&quot;用户名的长度是6-12位，请重新输入&quot;);</span></span><br><span class="line">                    <span class="comment">//静态的成员方法和成员变量是可以通过类名访问的</span></span><br><span class="line">                    JOptionPane.showMessageDialog(jf,<span class="string">&quot;用户名的长度是6-12位，请重新输入&quot;</span>);</span><br><span class="line">                    usernameField.setText(<span class="string">&quot;&quot;</span>);</span><br><span class="line">                    <span class="keyword">return</span>;</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">                <span class="comment">//再判断用户登录是否成功</span></span><br><span class="line">                <span class="keyword">if</span>(username.equals(name) &amp;&amp; password.equals(pwd)) &#123;</span><br><span class="line"><span class="comment">//                    System.out.println(&quot;登录成功&quot;);</span></span><br><span class="line">                    JOptionPane.showMessageDialog(jf,<span class="string">&quot;登录成功&quot;</span>);</span><br><span class="line">                    usernameField.setText(<span class="string">&quot;&quot;</span>);</span><br><span class="line">                    passwordField.setText(<span class="string">&quot;&quot;</span>);</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line"><span class="comment">//                    System.out.println(&quot;用户名或者密码有误&quot;);</span></span><br><span class="line">                    JOptionPane.showMessageDialog(jf,<span class="string">&quot;用户名或者密码有误&quot;</span>);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        jf.setVisible(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h3 id="gui案例2聊天室实现">1.4 GUI案例2(聊天室实现)</h3>
<p>需求：根据下列要求，完成聊天室的逻辑</p>
<figure>
<img data-src="imgs/1640164718346.png" alt="1640164718346">
<figcaption aria-hidden="true">1640164718346</figcaption>
</figure>
<p>要求：</p>
<p>① 把文本框的内容发送到文本域中</p>
<p>② 每次发送的文本内容不带前后空格</p>
<p>③ 多次发送的内容在文本域以追加的方式呈现</p>
<p>④ 清空聊天就是把文本域的内容设置为空</p>
<p>知道了聊天室案例的需求和要求后，下面我们到IDEA中去实现一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionEvent;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionListener;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    1:把文本框的内容发送到文本域中</span></span><br><span class="line"><span class="comment">    2:每次发送的文本内容不带前后空格</span></span><br><span class="line"><span class="comment">    3:多次发送的内容在文本域以追加的方式呈现</span></span><br><span class="line"><span class="comment">    4:清空聊天就是把文本域的内容设置为空</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ChatRoom</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建窗体对象</span></span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">jf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>();</span><br><span class="line"></span><br><span class="line">        jf.setTitle(<span class="string">&quot;聊天室&quot;</span>);</span><br><span class="line">        jf.setSize(<span class="number">400</span>, <span class="number">300</span>);</span><br><span class="line">        jf.setDefaultCloseOperation(<span class="number">3</span>);</span><br><span class="line">        jf.setLocationRelativeTo(<span class="literal">null</span>);</span><br><span class="line">        jf.setAlwaysOnTop(<span class="literal">true</span>);</span><br><span class="line">        jf.setLayout(<span class="literal">null</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//显示聊天信息的文本域</span></span><br><span class="line">        <span class="type">JTextArea</span> <span class="variable">messageArea</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JTextArea</span>();</span><br><span class="line">        messageArea.setBounds(<span class="number">10</span>, <span class="number">10</span>, <span class="number">360</span>, <span class="number">200</span>);</span><br><span class="line">        jf.add(messageArea);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//输入聊天信息的文本框</span></span><br><span class="line">        <span class="type">JTextField</span> <span class="variable">messageField</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JTextField</span>();</span><br><span class="line">        messageField.setBounds(<span class="number">10</span>, <span class="number">230</span>, <span class="number">180</span>, <span class="number">20</span>);</span><br><span class="line">        jf.add(messageField);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//发送按钮</span></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">sendButton</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">&quot;发送&quot;</span>);</span><br><span class="line">        sendButton.setBounds(<span class="number">200</span>, <span class="number">230</span>, <span class="number">70</span>, <span class="number">20</span>);</span><br><span class="line">        jf.add(sendButton);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//清空聊天按钮</span></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">clearButton</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">&quot;清空聊天&quot;</span>);</span><br><span class="line">        clearButton.setBounds(<span class="number">280</span>, <span class="number">230</span>, <span class="number">100</span>, <span class="number">20</span>);</span><br><span class="line">        jf.add(clearButton);</span><br><span class="line"></span><br><span class="line">        sendButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">                <span class="comment">//获取文本框的内容</span></span><br><span class="line"><span class="comment">//                String message = messageField.getText();</span></span><br><span class="line"><span class="comment">//                messageField.setText(&quot;&quot;);</span></span><br><span class="line"></span><br><span class="line">                <span class="comment">//每次发送的文本内容不带前后空格</span></span><br><span class="line"><span class="comment">//                String message = messageField.getText();</span></span><br><span class="line"><span class="comment">//                message = message.trim();</span></span><br><span class="line">                <span class="type">String</span> <span class="variable">message</span> <span class="operator">=</span> messageField.getText().trim(); <span class="comment">//链式调用</span></span><br><span class="line">                messageField.setText(<span class="string">&quot;&quot;</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">                <span class="comment">//把文本框的内容发送到文本域中</span></span><br><span class="line"><span class="comment">//                messageArea.setText(message);</span></span><br><span class="line">                <span class="comment">//多次发送的内容在文本域以追加的方式呈现</span></span><br><span class="line">                messageArea.append(message+<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        clearButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">                <span class="comment">//清空聊天就是把文本域的内容设置为空</span></span><br><span class="line">                messageArea.setText(<span class="string">&quot;&quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        jf.setVisible(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h2 id="integer">2：Integer</h2>
<h3 id="integer构造方法">2.1 Integer(构造方法)</h3>
<p>来，继续啊，下面我们来学习基本类型包装类，它涉及到1个案例：猜数字。</p>
<figure>
<img data-src="imgs/1640164796396.png" alt="1640164796396">
<figcaption aria-hidden="true">1640164796396</figcaption>
</figure>
<p>来，我们一起看一下，在这个输入框里，我们要输入一个猜的数据，然后点击我猜这个按钮，和已经产生的数据进行比对，给出相应的提示即可。</p>
<p>而且呢，猜数字案例的基本逻辑，我们前面是讲解过的，只不过当时使用的是键盘录入数据，而这里我们采用的是在输入框里输入数据而已。</p>
<p>那我们为什么要学习基本类型包装类呢？</p>
<p>因为，在输入框里输入的数据，我们得到的是一个字符串数据，而实际比较的时候我们需要的是一个int类型的数据，所以，我们得想办法把字符串数据转换为int类型的数据。</p>
<p>为了实现这个效果，Java就提供了基本类型包装类供我们使用。所以，我们说，将基本数据类型封装成对象的好处就是可以通过对象调用方法操作数据。</p>
<p>而我们常用的操作之一呢：就是用于基本数据类型与字符串之间的转换。</p>
<p>在这里呢，我们一起来看一下，八种基本数据类型和其对应的包装类类型</p>
<figure>
<img data-src="imgs/1640164856732.png" alt="1640164856732">
<figcaption aria-hidden="true">1640164856732</figcaption>
</figure>
<p>从名字上看，包装类类型就是把基本类型的第一个单词改为大写即可。</p>
<p>当然，这里也有两个比较特殊的，一个是int类型的包装类，一个是字符类型的包装类，它们的包装类名称是这个样子的，大家了解一下即可。</p>
<p>下面呢，我们以Integer举例来讲解字符串和int类型之间的相互转换，其他基本类型和字符串的转换方式类似，在使用的时候，大家参照Integer的使用即可。</p>
<p>而关于Integer类的学习呢，我们先到帮助文档中去查看，然后回来总结，最后到IDEA中去演示。</p>
<p>来，打开我们准备好的帮助文档，关于通过帮助文档查看Integer，大家按照老师讲解的API的使用步骤看看即可。</p>
<p>通过文档了解了Integer类之后，下面我们回到资料中总结一下：</p>
<p><strong>Integer：</strong></p>
<ul>
<li>Integer类在对象中包装基本类型int的值</li>
</ul>
<p><strong>构造方法：</strong></p>
<ul>
<li>Integer(int value)：根据 int 值创建 Integer 对象(过时)</li>
<li>Integer(String s)：根据 String 值创建 Integer 对象(过时)</li>
</ul>
<p><strong>成员方法：</strong></p>
<ul>
<li>static Integer valueOf(int i)：返回表示指定的 int 值的 Integer
实例</li>
<li>static Integer valueOf(String s)：返回一个保存指定值的 Integer 对象
String</li>
</ul>
<p>知道了Integer的构造方法后，我们到IDEA中去使用一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    构造方法</span></span><br><span class="line"><span class="comment">        Integer(int value)：根据 int 值创建 Integer 对象(过时)</span></span><br><span class="line"><span class="comment">        Integer(String s)：根据 String 值创建 Integer 对象(过时)</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    成员方法</span></span><br><span class="line"><span class="comment">        static Integer valueOf(int i)：返回表示指定的 int 值的 Integer 实例</span></span><br><span class="line"><span class="comment">        static Integer valueOf(String s)：返回一个保存指定值的 Integer 对象 String</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">IntegerDemo01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line"><span class="comment">//        //Integer(int value)：根据 int 值创建 Integer 对象(过时)</span></span><br><span class="line"><span class="comment">//        Integer i1 = new Integer(100);</span></span><br><span class="line"><span class="comment">//        System.out.println(i1);</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//        //Integer(String s)：根据 String 值创建 Integer 对象(过时)</span></span><br><span class="line"><span class="comment">//        Integer i2 = new Integer(&quot;100&quot;);</span></span><br><span class="line"><span class="comment">//        System.out.println(i2);</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//        static Integer valueOf(int i)：返回表示指定的 int 值的 Integer 实例</span></span><br><span class="line"><span class="comment">//        static Integer valueOf(String s)：返回一个保存指定值的 Integer 对象 String</span></span><br><span class="line">        <span class="type">Integer</span> <span class="variable">i1</span> <span class="operator">=</span> Integer.valueOf(<span class="number">100</span>);</span><br><span class="line">        <span class="type">Integer</span> <span class="variable">i2</span> <span class="operator">=</span> Integer.valueOf(<span class="string">&quot;100&quot;</span>);</span><br><span class="line">        System.out.println(i1);</span><br><span class="line">        System.out.println(i2);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h3 id="integerint和string的相互转换">2.2
Integer(int和String的相互转换)</h3>
<p>来，继续啊，下面我们来学习int类型数据和String类型数据的相互转换，关于它们之间的相互转换，我们先到IDEA中去讲解，然后回来总结。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    int和String的相互转换</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">IntegerDemo02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//int --- String</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">number</span> <span class="operator">=</span> <span class="number">100</span>;</span><br><span class="line">        <span class="comment">//方式1</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s1</span> <span class="operator">=</span> number + <span class="string">&quot;&quot;</span>;</span><br><span class="line">        System.out.println(s1);</span><br><span class="line">        <span class="comment">//方式2</span></span><br><span class="line">        <span class="comment">//static String valueOf(int i)</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s2</span> <span class="operator">=</span> String.valueOf(number);</span><br><span class="line">        System.out.println(s2);</span><br><span class="line">        System.out.println(<span class="string">&quot;---------------&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//String --- int</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> <span class="string">&quot;100&quot;</span>;</span><br><span class="line">        <span class="comment">//方式1：String --- Integer --- int</span></span><br><span class="line">        <span class="type">Integer</span> <span class="variable">i</span> <span class="operator">=</span> Integer.valueOf(s);</span><br><span class="line">        <span class="comment">//int intValue() 返回此值 Integer为 int</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">x</span> <span class="operator">=</span> i.intValue();</span><br><span class="line">        System.out.println(x);</span><br><span class="line">        <span class="comment">//方式2</span></span><br><span class="line">        <span class="comment">//static int parseInt(String s)</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">y</span> <span class="operator">=</span> Integer.parseInt(s);</span><br><span class="line">        System.out.println(y);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕int类型和String类型的相互转换后，回到资料总结一下：</p>
<ul>
<li><strong>int 转换为 String</strong>
<ul>
<li><font color="red"><strong>static String valueOf(int
i)：</strong></font>返回 int 参数的字符串表示形式。该方法是 String
类中的方法</li>
</ul></li>
<li><strong>String 转换为 int</strong>
<ul>
<li><font color="red"><strong>static int parseInt(String
s)：</strong></font>将字符串解析为 int 类型。该方法是 Integer
类中的方法</li>
</ul></li>
</ul>
<p>好了，int类型数据和String类型数据的相互转换我们就先讲到这里。</p>
<h3 id="gui案例3猜数字实现">2.3 GUI案例3(猜数字实现)</h3>
<p>需求：根据下列要求，完成猜数字的逻辑</p>
<figure>
<img data-src="imgs/1640165180138.png" alt="1640165180138">
<figcaption aria-hidden="true">1640165180138</figcaption>
</figure>
<p>要求：</p>
<p>① 系统产生一个1-100之间的随机数</p>
<p>② 猜的内容不能为空</p>
<p>③ 每次根据猜的数字给出相应的提示</p>
<p>知道了猜数字案例的需求和要求后，下面我们到IDEA中去实现一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionEvent;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionListener;</span><br><span class="line"><span class="keyword">import</span> java.util.Random;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    1:系统产生一个1-100之间的随机数</span></span><br><span class="line"><span class="comment">    2:猜的内容不能为空</span></span><br><span class="line"><span class="comment">    3:每次根据猜的数字给出相应的提示</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">GuessNumber</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建窗体对象</span></span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">jf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>();</span><br><span class="line"></span><br><span class="line">        jf.setTitle(<span class="string">&quot;猜数字&quot;</span>);</span><br><span class="line">        jf.setSize(<span class="number">400</span>, <span class="number">300</span>);</span><br><span class="line">        jf.setDefaultCloseOperation(<span class="number">3</span>);</span><br><span class="line">        jf.setLocationRelativeTo(<span class="literal">null</span>);</span><br><span class="line">        jf.setAlwaysOnTop(<span class="literal">true</span>);</span><br><span class="line">        jf.setLayout(<span class="literal">null</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//系统产生一个1-100之间的随机数</span></span><br><span class="line">        <span class="type">Random</span> <span class="variable">r</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Random</span>();</span><br><span class="line">        <span class="type">int</span> <span class="variable">number</span> <span class="operator">=</span> r.nextInt(<span class="number">100</span>) + <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//提示信息</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">messageLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;系统产生了一个1-100之间的数据，请猜一猜&quot;</span>);</span><br><span class="line">        messageLable.setBounds(<span class="number">70</span>, <span class="number">50</span>, <span class="number">350</span>, <span class="number">20</span>);</span><br><span class="line">        jf.add(messageLable);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//输入要猜的数字</span></span><br><span class="line">        <span class="type">JTextField</span> <span class="variable">numberField</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JTextField</span>();</span><br><span class="line">        numberField.setBounds(<span class="number">120</span>, <span class="number">100</span>, <span class="number">150</span>, <span class="number">20</span>);</span><br><span class="line">        jf.add(numberField);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//猜数字的按钮</span></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">guessButton</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">&quot;我猜&quot;</span>);</span><br><span class="line">        guessButton.setBounds(<span class="number">150</span>, <span class="number">150</span>, <span class="number">100</span>, <span class="number">20</span>);</span><br><span class="line">        jf.add(guessButton);</span><br><span class="line"></span><br><span class="line">        guessButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">                <span class="comment">//猜的内容不能为空</span></span><br><span class="line">                <span class="type">String</span> <span class="variable">stringNumber</span> <span class="operator">=</span> numberField.getText().trim();</span><br><span class="line">                <span class="keyword">if</span>(stringNumber.equals(<span class="string">&quot;&quot;</span>)) &#123;</span><br><span class="line">                    JOptionPane.showMessageDialog(jf,<span class="string">&quot;猜的数字不能为空&quot;</span>);</span><br><span class="line">                    <span class="keyword">return</span>;</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">                <span class="comment">//每次根据猜的数字给出相应的提示</span></span><br><span class="line">                <span class="type">int</span> <span class="variable">guessNumber</span> <span class="operator">=</span> Integer.parseInt(stringNumber);</span><br><span class="line"></span><br><span class="line">                <span class="keyword">if</span>(guessNumber &gt; number) &#123;</span><br><span class="line">                    JOptionPane.showMessageDialog(jf,<span class="string">&quot;你猜的数字&quot;</span>+guessNumber+<span class="string">&quot;大了&quot;</span>);</span><br><span class="line">                    numberField.setText(<span class="string">&quot;&quot;</span>);</span><br><span class="line">                &#125; <span class="keyword">else</span> <span class="keyword">if</span>(guessNumber &lt; number) &#123;</span><br><span class="line">                    JOptionPane.showMessageDialog(jf,<span class="string">&quot;你猜的数字&quot;</span>+guessNumber+<span class="string">&quot;小了&quot;</span>);</span><br><span class="line">                    numberField.setText(<span class="string">&quot;&quot;</span>);</span><br><span class="line">                &#125; <span class="keyword">else</span>  &#123;</span><br><span class="line">                    JOptionPane.showMessageDialog(jf,<span class="string">&quot;恭喜你猜中了&quot;</span>);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        jf.setVisible(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h3 id="integer自动装箱和拆箱">2.4 Integer(自动装箱和拆箱)</h3>
<p>来，继续啊，下面我们来学习自动装箱和拆箱。</p>
<p>首先，我们来说一下什么是装箱和拆箱：</p>
<ul>
<li><strong>装箱：</strong>把基本数据类型转换为对应的包装类类型</li>
<li><strong>拆箱：</strong>把包装类类型转换为对应的基本数据类型</li>
</ul>
<p>了解了装箱和拆箱后，下面我们到IDEA中去演示一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    装箱：把基本数据类型转换为对应的包装类类型</span></span><br><span class="line"><span class="comment">    拆箱：把包装类类型转换为对应的基本数据类型</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">IntegerDemo03</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//装箱：把基本数据类型转换为对应的包装类类型</span></span><br><span class="line"><span class="comment">//        Integer i = Integer.valueOf(100);</span></span><br><span class="line">        <span class="type">Integer</span> <span class="variable">ii</span> <span class="operator">=</span> <span class="number">100</span>; <span class="comment">//Integer.valueOf(100);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//拆箱：把包装类类型转换为对应的基本数据类型</span></span><br><span class="line">        ii += <span class="number">200</span>;</span><br><span class="line">        <span class="comment">/*</span></span><br><span class="line"><span class="comment">            ii = ii + 200;</span></span><br><span class="line"><span class="comment">            ii = ii.intValue() + 200;   //自动拆箱</span></span><br><span class="line"><span class="comment">            ii = 300; //Integer.valueOf(300);</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">        System.out.println(ii);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>好了，自动装箱和拆箱我们就先讲到这里。</p>
<h2 id="date和simpledateformat">3：Date和SimpleDateFormat</h2>
<h3 id="date构造方法">3.1 Date(构造方法)</h3>
<p>来，继续啊，下面我们来学习Date类和SimpleDateFormat类，它们涉及到两个案例：手机日期和时间显示和考勤查询</p>
<p>先来看手机日期和时间显示案例：获取当前时间，按照指定的格式显示在窗体上。</p>
<p>也就是在这里我们要进行把一个Date类型的数据，按照指定的格式，变成一个字符串数据显示出来。</p>
<figure>
<img data-src="imgs/1640165405894.png" alt="1640165405894">
<figcaption aria-hidden="true">1640165405894</figcaption>
</figure>
<p>再来看考勤查询案例，我们要通过输入的指定格式的日期字符串数据，得到Date类型的数据。</p>
<p>所以，我们学习Date类和SimpleDateFormat类的重点就是为了把Date类型数据和String类型数据之间进行按照指定格式进行转换。</p>
<figure>
<img data-src="imgs/1640165411817.png" alt="1640165411817">
<figcaption aria-hidden="true">1640165411817</figcaption>
</figure>
<p>首先呢，我们先来学习一下Date类，而关于Date类呢，我们先到帮助文档中去查看，然后回来总结，最后到IDEA中去演示。</p>
<p>来，打开我们准备好的帮助文档，关于通过帮助文档查看Date，大家按照老师讲解的API的使用步骤看看即可。</p>
<p>通过文档了解了Date类之后，下面我们回到资料中总结一下：</p>
<p><strong>Date：</strong></p>
<ul>
<li>Date类表示特定的时刻，精度为毫秒</li>
</ul>
<p><strong>构造方法：</strong></p>
<ul>
<li>Date()：分配 Date对象并对其进行初始化，使其表示分配时间，测量
Date到毫秒</li>
<li>Date(long date)：分配
Date对象并初始化它以表示自<font color="red"><strong>标准基准时间</strong></font>以来的指定毫秒数，即<font color="red"><strong>1970年1月1日00:00:00</strong></font></li>
</ul>
<p>知道了(的构造方法后，我们到IDEA中去使用一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    构造方法：</span></span><br><span class="line"><span class="comment">        Date()：分配 Date对象并对其进行初始化，使其表示分配时间，测量 Date到毫秒</span></span><br><span class="line"><span class="comment">        Date(long date)：分配 Date对象并初始化它以表示自标准基准时间以来的指定毫秒数，即1970年1月1日00:00:00</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">DateDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//Date()：分配 Date对象并对其进行初始化，使其表示分配时间，测量 Date到毫秒</span></span><br><span class="line">        <span class="type">Date</span> <span class="variable">d1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Date</span>();</span><br><span class="line">        System.out.println(d1);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//Date(long date)：分配 Date对象并初始化它以表示自标准基准时间以来的指定毫秒数，即1970年1月1日00:00:00</span></span><br><span class="line">        <span class="type">Date</span> <span class="variable">d2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Date</span>(<span class="number">1000</span> * <span class="number">60</span> * <span class="number">60</span>);</span><br><span class="line">        System.out.println(d2);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>而目前呢，Date类中没有我们要使用的成员方法，所以，我们也就不看它的成员方法了。</p>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h3 id="simpledateformatdate和string的相互转换">3.2
SimpleDateFormat(Date和String的相互转换)</h3>
<p>来，继续啊，下面我们来学习SimpleDateFormat这个类。</p>
<p>打开我们准备好的帮助文档，关于通过帮助文档查看SimpleDateFormat，大家按照老师讲解的API的使用步骤看看即可。</p>
<p>通过文档了解了SimpleDateFormat类之后，下面我们回到资料中总结一下：</p>
<p><strong>SimpleDateFormat：</strong></p>
<ul>
<li><p>SimpleDateFormat
是一个用于以区域设置敏感的方式格式化和解析日期的具体类。我们重点学习日期格式化和解析</p></li>
<li><p>日期和时间格式由日期和时间模式字符串指定，在日期和时间模式字符串中，从‘A’到‘Z’以及从‘a’到‘z’引号的字母被解释为表示日期或时间字符串的组成部分的模式字母</p></li>
</ul>
<p>常用的模式字母及对应关系如下：</p>
<figure>
<img data-src="imgs/1640165745241.png" alt="1640165745241">
<figcaption aria-hidden="true">1640165745241</figcaption>
</figure>
<ul>
<li><strong>举例：</strong>2021年10月27日 11:11:11</li>
<li><strong>模式：</strong>yyyy年MM月dd日 HH:mm:ss</li>
</ul>
<p><strong>构造方法：</strong></p>
<ul>
<li>SimpleDateFormat()：构造一个SimpleDateFormat，使用默认模式和日期格式</li>
<li>SimpleDateFormat(String
pattern)：构造一个SimpleDateFormat使用给定的模式和默认的日期格式</li>
</ul>
<p><strong>格式化(从 Date 到 String )：</strong></p>
<ul>
<li>String format(Date date)：将日期格式化成日期/时间字符串</li>
</ul>
<p><strong>解析(从 String 到 Date )：</strong></p>
<ul>
<li>Date parse(String source)：从给定字符串的开始解析文本以生成日期</li>
</ul>
<p>了解了SimpleDateFormat类，以及Date和String相互转换的方法后，我们到IDEA中去体验一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.text.ParseException;</span><br><span class="line"><span class="keyword">import</span> java.text.SimpleDateFormat;</span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    构造方法</span></span><br><span class="line"><span class="comment">        SimpleDateFormat()：构造一个SimpleDateFormat，使用默认模式和日期格式</span></span><br><span class="line"><span class="comment">        SimpleDateFormat(String pattern)：构造一个SimpleDateFormat使用给定的模式和默认的日期格式</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    格式化(从 Date 到 String )</span></span><br><span class="line"><span class="comment">        String format(Date date)：将日期格式化成日期/时间字符串</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">    解析(从 String 到 Date )</span></span><br><span class="line"><span class="comment">        Date parse(String source)：从给定字符串的开始解析文本以生成日期</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">SimpleDateFormatDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> <span class="keyword">throws</span> ParseException &#123;</span><br><span class="line">        <span class="comment">//格式化(从 Date 到 String )</span></span><br><span class="line">        <span class="type">Date</span> <span class="variable">d</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Date</span>();</span><br><span class="line">        <span class="comment">//SimpleDateFormat()：构造一个SimpleDateFormat，使用默认模式和日期格式</span></span><br><span class="line"><span class="comment">//        SimpleDateFormat sdf = new SimpleDateFormat();</span></span><br><span class="line">        <span class="comment">//SimpleDateFormat(String pattern)：构造一个SimpleDateFormat使用给定的模式和默认的日期格式</span></span><br><span class="line">        <span class="type">SimpleDateFormat</span> <span class="variable">sdf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SimpleDateFormat</span>(<span class="string">&quot;yyyy年MM月dd日 HH:mm:ss&quot;</span>);</span><br><span class="line">        <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> sdf.format(d);</span><br><span class="line">        System.out.println(s);</span><br><span class="line">        System.out.println(<span class="string">&quot;-----------------&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//解析(从 String 到 Date )</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">ss</span> <span class="operator">=</span> <span class="string">&quot;2021-10-27 11:11:11&quot;</span>;</span><br><span class="line"><span class="comment">//        SimpleDateFormat sdf2 = new SimpleDateFormat(&quot;yyyy年MM月dd日 HH:mm:ss&quot;);</span></span><br><span class="line">        <span class="type">SimpleDateFormat</span> <span class="variable">sdf2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SimpleDateFormat</span>(<span class="string">&quot;yyyy-MM-dd HH:mm:ss&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">Date</span> <span class="variable">dd</span> <span class="operator">=</span> sdf2.parse(ss);</span><br><span class="line">        System.out.println(dd);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h3 id="gui案例4手机日期和时间显示实现">3.3
GUI案例4(手机日期和时间显示实现)</h3>
<p>需求：根据下列要求，完成手机日期和时间显示</p>
<figure>
<img data-src="imgs/1640165873028.png" alt="1640165873028">
<figcaption aria-hidden="true">1640165873028</figcaption>
</figure>
<p>要求：以当前时间根据格式分别获取日期和时间显示</p>
<p>知道了手机日期和时间显示案例的需求和要求后，下面我们到IDEA中去实现一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">import</span> java.text.SimpleDateFormat;</span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    手机日期和时间显示</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ShowDateTime</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建窗体对象</span></span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">jf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>();</span><br><span class="line"></span><br><span class="line">        jf.setTitle(<span class="string">&quot;手机日期和时间显示&quot;</span>);</span><br><span class="line">        jf.setSize(<span class="number">400</span>, <span class="number">300</span>);</span><br><span class="line">        jf.setDefaultCloseOperation(<span class="number">3</span>);</span><br><span class="line">        jf.setLocationRelativeTo(<span class="literal">null</span>);</span><br><span class="line">        jf.setAlwaysOnTop(<span class="literal">true</span>);</span><br><span class="line">        jf.setLayout(<span class="literal">null</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//提示日期</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">dateLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;日期&quot;</span>);</span><br><span class="line">        dateLable.setBounds(<span class="number">50</span>, <span class="number">50</span>, <span class="number">100</span>, <span class="number">20</span>);</span><br><span class="line">        jf.add(dateLable);</span><br><span class="line"></span><br><span class="line">        <span class="type">Date</span> <span class="variable">d</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Date</span>();</span><br><span class="line"></span><br><span class="line">        <span class="type">SimpleDateFormat</span> <span class="variable">sdf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SimpleDateFormat</span>(<span class="string">&quot;yyyy年MM月dd日&quot;</span>);</span><br><span class="line">        <span class="type">String</span> <span class="variable">dateString</span> <span class="operator">=</span> sdf.format(d);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//按照格式显示日期的字符串</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">showDateLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(dateString);</span><br><span class="line">        showDateLable.setBounds(<span class="number">50</span>, <span class="number">80</span>, <span class="number">200</span>, <span class="number">20</span>);</span><br><span class="line">        jf.add(showDateLable);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//提示时间</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">timeLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;时间&quot;</span>);</span><br><span class="line">        timeLable.setBounds(<span class="number">50</span>, <span class="number">150</span>, <span class="number">100</span>, <span class="number">20</span>);</span><br><span class="line">        jf.add(timeLable);</span><br><span class="line"></span><br><span class="line">        <span class="type">SimpleDateFormat</span> <span class="variable">sdf2</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SimpleDateFormat</span>(<span class="string">&quot;HH:mm&quot;</span>);</span><br><span class="line">        <span class="type">String</span> <span class="variable">timeString</span> <span class="operator">=</span> sdf2.format(d);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//按照格式显示时间的字符串</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">showTimeLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(timeString);</span><br><span class="line">        showTimeLable.setBounds(<span class="number">50</span>, <span class="number">180</span>, <span class="number">200</span>, <span class="number">20</span>);</span><br><span class="line">        jf.add(showTimeLable);</span><br><span class="line"></span><br><span class="line">        jf.setVisible(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h3 id="gui案例5考勤查询实现">3.4 GUI案例5(考勤查询实现)</h3>
<p>需求：根据下列要求，完成考勤查询的逻辑</p>
<figure>
<img data-src="imgs/1640165952879.png" alt="1640165952879">
<figcaption aria-hidden="true">1640165952879</figcaption>
</figure>
<p>要求：根据开始时间和结束时间的字符串解析为日期提示出来</p>
<p>知道了考勤查询案例的需求和要求后，下面我们到IDEA中去实现一下：</p>
<p><font color="red"><strong>这里用到了日历控件类，需要大家把DateChooser.java类准备好</strong></font></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionEvent;</span><br><span class="line"><span class="keyword">import</span> java.awt.event.ActionListener;</span><br><span class="line"><span class="keyword">import</span> java.text.ParseException;</span><br><span class="line"><span class="keyword">import</span> java.text.SimpleDateFormat;</span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    考勤查询</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">AttendanceQuery</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建窗体对象</span></span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">jf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>();</span><br><span class="line"></span><br><span class="line">        jf.setTitle(<span class="string">&quot;考勤查询&quot;</span>);</span><br><span class="line">        jf.setSize(<span class="number">400</span>, <span class="number">300</span>);</span><br><span class="line">        jf.setDefaultCloseOperation(<span class="number">3</span>);</span><br><span class="line">        jf.setLocationRelativeTo(<span class="literal">null</span>);</span><br><span class="line">        jf.setAlwaysOnTop(<span class="literal">true</span>);</span><br><span class="line">        jf.setLayout(<span class="literal">null</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//显示考勤日期的文本</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">dateLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;考勤日期&quot;</span>);</span><br><span class="line">        dateLable.setBounds(<span class="number">50</span>, <span class="number">20</span>, <span class="number">100</span>, <span class="number">20</span>);</span><br><span class="line">        jf.add(dateLable);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//显示开始时间文本</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">startDateLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;开始时间&quot;</span>);</span><br><span class="line">        startDateLable.setBounds(<span class="number">50</span>, <span class="number">70</span>, <span class="number">100</span>, <span class="number">20</span>);</span><br><span class="line">        jf.add(startDateLable);</span><br><span class="line"></span><br><span class="line">        <span class="type">DateChooser</span> <span class="variable">dateChooser1</span> <span class="operator">=</span> DateChooser.getInstance(<span class="string">&quot;yyyy/MM/dd&quot;</span>);</span><br><span class="line">        <span class="type">DateChooser</span> <span class="variable">dateChooser2</span> <span class="operator">=</span> DateChooser.getInstance(<span class="string">&quot;yyyy/MM/dd&quot;</span>);</span><br><span class="line">        <span class="comment">//开始时间输入框</span></span><br><span class="line">        <span class="type">JTextField</span> <span class="variable">startDateField</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JTextField</span>();</span><br><span class="line">        startDateField.setBounds(<span class="number">50</span>, <span class="number">100</span>, <span class="number">100</span>, <span class="number">20</span>);</span><br><span class="line">        dateChooser1.register(startDateField);</span><br><span class="line">        jf.add(startDateField);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//显示结束时间文本</span></span><br><span class="line">        <span class="type">JLabel</span> <span class="variable">endDateLable</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="string">&quot;结束时间&quot;</span>);</span><br><span class="line">        endDateLable.setBounds(<span class="number">250</span>, <span class="number">70</span>, <span class="number">100</span>, <span class="number">20</span>);</span><br><span class="line">        jf.add(endDateLable);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//结束时间输入框</span></span><br><span class="line">        <span class="type">JTextField</span> <span class="variable">endDateField</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JTextField</span>();</span><br><span class="line">        endDateField.setBounds(<span class="number">250</span>, <span class="number">100</span>, <span class="number">100</span>, <span class="number">20</span>);</span><br><span class="line">        dateChooser2.register(endDateField);</span><br><span class="line">        jf.add(endDateField);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//确定按钮</span></span><br><span class="line">        <span class="type">JButton</span> <span class="variable">confirmButton</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JButton</span>(<span class="string">&quot;确定&quot;</span>);</span><br><span class="line">        confirmButton.setBounds(<span class="number">250</span>, <span class="number">180</span>, <span class="number">60</span>, <span class="number">20</span>);</span><br><span class="line">        jf.add(confirmButton);</span><br><span class="line"></span><br><span class="line">        confirmButton.addActionListener(<span class="keyword">new</span> <span class="title class_">ActionListener</span>() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">actionPerformed</span><span class="params">(ActionEvent e)</span> &#123;</span><br><span class="line">                <span class="type">SimpleDateFormat</span> <span class="variable">sdf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">SimpleDateFormat</span>(<span class="string">&quot;yyyy/MM/dd&quot;</span>);</span><br><span class="line"></span><br><span class="line">                <span class="comment">//获取开始时间和结束时间的字符串</span></span><br><span class="line">                <span class="type">String</span> <span class="variable">startDateString</span> <span class="operator">=</span> startDateField.getText();</span><br><span class="line">                <span class="type">String</span> <span class="variable">endDateString</span> <span class="operator">=</span> endDateField.getText();</span><br><span class="line"></span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    <span class="type">Date</span> <span class="variable">startDate</span> <span class="operator">=</span> sdf.parse(startDateString);</span><br><span class="line">                    <span class="type">Date</span> <span class="variable">endDate</span> <span class="operator">=</span> sdf.parse(endDateString);</span><br><span class="line"></span><br><span class="line">                    JOptionPane.showMessageDialog(jf,startDate+<span class="string">&quot;\n&quot;</span>+endDate);</span><br><span class="line">                &#125; <span class="keyword">catch</span> (ParseException ex) &#123;</span><br><span class="line">                    ex.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line"></span><br><span class="line">        jf.setVisible(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这里我们仅仅是演示了考勤查询的日期数据处理，将来呢，我们就可以根据这两个日期数据到考勤查询系统中去查询考勤信息了。</p>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="reward-container">
  <div>请我一杯咖啡吧！</div>
  <button>
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wChat.png" alt="Ethan Liu 微信">
        <span>微信</span>
      </div>

  </div>
</div>

          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Ethan Liu
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="http://ethanliu6.github.io/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/" title="JavaSE基础——Day07 JavaSE基础——常用API:String、Integer、Date、SDF">http://ethanliu6.github.io/2024/03/21/JavaNotes/JavaSE基础/day07常用API(2)/</a>
  </li>
  <li class="post-copyright-license">
      <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzQuMC8="><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


          <div class="post-tags">
              <a href="/tags/Java/" rel="tag"><i class="fa fa-tag"></i> Java</a>
              <a href="/tags/Java%E5%9F%BA%E7%A1%80/" rel="tag"><i class="fa fa-tag"></i> Java基础</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2024/03/19/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day06%E5%B8%B8%E7%94%A8API(1)/" rel="prev" title="JavaSE基础——Day06 JavaSE基础——常用API:Scanner、Random、GUI">
                  <i class="fa fa-angle-left"></i> JavaSE基础——Day06 JavaSE基础——常用API:Scanner、Random、GUI
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/" rel="next" title="JavaSE基础——Day08 JavaSE基础——数组">
                  JavaSE基础——Day08 JavaSE基础——数组 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="SOHUCS" sid="44fc5a735838d3c51243aaf6d238fea3"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2023-2024 – 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Is Ethan's</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">113k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">6:51</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL0V0aGFuTGl1Ni8=" title="在 GitHub 上关注我" aria-label="在 GitHub 上关注我"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.33/fancybox/fancybox.umd.js" integrity="sha256-+2+qOqR8CKoHh/AsVR9k2qaDBKWjYNC2nozhYmv5j9k=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/medium-zoom/1.1.0/medium-zoom.min.js" integrity="sha256-ZgMyDAIYDYGxbcpJcfUnYwNevG/xi9OHKaR/8GK+jWc=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>

  <script class="next-config" data-name="pdf" type="application/json">{"object_url":{"url":"https://cdnjs.cloudflare.com/ajax/libs/pdfobject/2.2.12/pdfobject.min.js","integrity":"sha256-g2xji1rlE3KsGVClvuxTbcR0Kn2+wtQADSff2Tbb4zA="},"url":"/lib/pdf/web/viewer.html"}</script>
  <script src="/js/third-party/tags/pdf.js"></script>



  <script src="/js/third-party/fancybox.js"></script>



  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"all","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


  <script src="https://cdnjs.cloudflare.com/ajax/libs/quicklink/2.3.0/quicklink.umd.js" integrity="sha256-yvJQOINiH9fWemHn0vCA5lsHWJaHs6/ZmO+1Ft04SvM=" crossorigin="anonymous"></script>
  <script class="next-config" data-name="quicklink" type="application/json">{"enable":true,"home":true,"archive":true,"delay":true,"timeout":3000,"priority":true,"url":"http://ethanliu6.github.io/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/"}</script>
  <script src="/js/third-party/quicklink.js"></script>
<script class="next-config" data-name="changyan" type="application/json">{"enable":true,"appid":"cyxf6tytI","appkey":"90987f30c4b5248baad5a04c7a6abfe1","count":true}</script>
<script src="/js/third-party/comments/changyan.js"></script>

</body>
</html>
